
class SearchController < ApplicationController
  def search_view
      @bam = 2
      if session[:uid].nil?
        redirect_to :action=>"login", :controller=>"user"
      end
      if session[:show].nil?
        @result=Book.all
      else
        @result=session[:show]
      end
      if session[:nr].nil?
        session[:nr]=@result.length
      end
      @notice=session[:nr].to_s + " results"
      if (session[:page].nil?)
        session[:page]=0
      end
      @from=session[:page].to_i*10
      @to=session[:page].to_i*10+9
      if (@to>=session[:nr].to_i)
        @to=session[:nr].to_i-1
      end
      session[:show]=nil
      
  end
  
  def search_string(a,b)
    return (a.upcase.strip==b.upcase.strip)
  end
  
  def search_process
    
    @all=Book.all
    @results=Array.new()
    @count=0
    @all.each do |r|
      if params[:id].empty? or r.id.to_i==params[:id].to_i
        if params[:name].empty? or search_string(r.Name.to_s,params[:name].to_s)==true
          if params[:author].empty? or search_string(r.Author.to_s,params[:author].to_s)==true
            if params[:genre].empty? or search_string(r.Genre.to_s,params[:genre].to_s)==true
              if params[:publish_year].empty? or r.Publish_year.to_i==params[:publish_year].to_i 
                @results << r
                @count=@count+1
              end
            end
          end
        end
      end 
    end 
    session[:show]=nil
    session[:show]=@results
    session[:nr]=@count
    session[:page]=0
    redirect_to :action => 'search_view'
    
  end
  
  def show_process
    session[:showid]=params[:id]
    redirect_to :action => 'show_view'
  end
  
  def show_view
    @showid=session[:showid]
    @showitem=Book.find_by_id(@showid)
    @s="".to_s
    @likeman=Like.find_all_by_bid(@showid)
    @likeme=Like.find_by_uid_and_bid(session[:uid],@showid)
    @all=@likeman.length
    @count=0
    if !@likeme.nil?
      session[:canUnlike]=@likeme
      @s=@s+"me, "
      @all-=1
      @count+=1
    end
    @likeman.each do |l|
      if l.uid.to_i!=session[:uid].to_i
        @s=@s+User.find_by_id(l.uid).Name.to_s
        @all-=1
        if (@all>0)
          @s=@s+", "
        end
        @count+=1
        if @count==3
          break
        end
      end
    end
    if (@all>0)
      @s=@s+"and "+@all.to_s+" other"
    end
    if (@all>1)
      @s=@s+"s"
    end
    if (@count==0)
      @s="None"
    end
    @comment= Comment.find_all_by_Bid(@showid)
  end
  
   def like
    @target=Like.new
    @target.uid=session[:uid]
    @target.bid=session[:showid]
    @target.save
    redirect_to :action=>'show_view'
  end
  
  def unlike
    session[:canUnlike].destroy
    redirect_to :action=>'show_view'
  end
  
  
  
  def order_process
    if params[:avail].to_i==0
      redirect_to :action => 'show_view',  :alert =>"No book avaiable now"
      return
    else 
      @a=Borrowingreport.find_by_Uid_and_Bid(session[:uid],params[:id])
      if @a==nil or @a.Return_date!=nil
         @target=Book.find_by_id(params[:id])
        @date=Borrowingreport.new
        @date.Uid=session[:uid]
        @date.Bid=params[:id]
       @date.Status="Order"
       
       @date.Borrow_date=(Time.now).to_s(:db)
        @date.Deadline=(Time.now+4.day).to_s(:db)
       @date.isExtend=0
         @date.save
        @target.update_attributes(:Available_quantity => params[:avail].to_i-1)
        redirect_to :action => 'show_view', :alert1 => "You have ordered "+@target.Name
      else
        redirect_to :action => 'show_view',  :alert =>"You can't order this book now"
      end
    end
  end
  
  def show_profile
    @bam = 1
    if session[:uid].nil?
      redirect_to :action=>"login", :controller=>"user" 
    end
    @showuser=User.find_by_id(session[:uid])
  end
  
  def service
    @bam = 4
    @sv=Servicereport.new
  end
  
  def sv_order
    @ser=params[:ins]
    @docu=params[:bname]
    @user = User.find_by_id(session[:uid])
    
    @n=Servicereport.new
    @strig="User: "+@user.Name+" Id: "+@user.id.to_s+"\\Address: "+@user.Address+"\\Phone: "+@user.Email+"\\Service order: "+@ser.to_s+"\\Document: "+@docu
    @n.Uid=session[:uid]
    @n.Status="order"
    @n.Content=@strig
    
    if @n.save
     redirect_to :action=>"service", :alert1 => "Order service is success"
    else 
      redirect_to :action=>"service", :alert => "Order service is fail"
    end
  end
  
  def logout
    session[:uid]=nil
    redirect_to :action=>"login", :controller=>"user", :alert1 => "User logout"
  end
  
  def change_page
    session[:page]=session[:page].to_i+params[:type].to_i
    if (session[:page].to_i<0) 
      session[:page]=0
    end
    if (session[:page].to_i*10>=session[:nr].to_i)
      session[:page]=session[:page].to_i-1
    end
    redirect_to :action=>'search_view'
  end
  
  def history
    @bam = 3
    @user = User.find_by_id(session[:uid])
    @listall = Borrowingreport.find_all_by_Uid(@user.id)
    if(params[:s]==nil)
      @status='Order'
     else
       @status=params[:s].to_str
    end
  end
  
  def xoaorder
    @conde = Borrowingreport.find_by_id(params[:o])
    @boo=Book.find_by_id(@conde.Bid)
    
    @boo.update_attributes(:Available_quantity => @boo.Available_quantity+1)
    @conde.destroy
    redirect_to :action=>'history',:alert =>"Book order canceled"
  end
  
  def extend
    @conde = Borrowingreport.find_by_id(params[:o])
    if @conde.isExtend != 0
      redirect_to :action=>'history',:s=>"dangmuon",:alert =>"Already extended"
      
    else
      @conbe=Book.find_by_id(@conde.Bid)
      if @conbe.Available_quantity>0 and @conde.Deadline.future?
        @conde.Deadline=(@conde.Deadline+4.day).to_s(:db)
        @conde.isExtend = 1
        @conde.save
        redirect_to :action=>'history',:s=>"dangmuon",:alert1 =>"Deadline extend is successful"
      else
        redirect_to :action=>'history',:s=>"dangmuon",:alert =>"Deadline extend fail"
      end
    end
  end
  
  def thaydoi
   
  end
   def changepass 
     end
  
  def cp
     @user = User.find(session[:uid]) 
    if params[:old_password] == @user.Password
      if(params[:new_password_confirmation] == params[:new_password])
        @user.Password = params[:new_password] 
        if @user.save
          redirect_to :action => 'show_profile',:alert1=>'Your password has been changed'
         else
             redirect_to :action => 'changepass',:alert => 'Unable to change your password'
         end 
      else
          redirect_to :action => 'changepass',:alert => 'Password confirm not match'
    end
    else
       redirect_to :action => 'changepass',:alert => 'Invaild password'
     
    end

  end
  
   def editprofile
     @user = User.find(session[:uid]) 
  end
  
  def updateprofile
    @user = User.find(session[:uid]) 
      if @user.update_attributes(params[:user])
         redirect_to :action => 'show_profile'
      else
         @user = User.find(:all)
         render :action => 'editprofile'
      end
  end
  
  def sendcomment
    @cm = Comment.new
    @cm.Uid=session[:uid]
    @cm.Bid=session[:showid]
    @cm.Content=params[:noidung]
    @cm.save
    redirect_to :action=>'show_view'
  end
  
end
